import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class SDSLLoader 
{
	private DBConnect dbConector;
	private String[] SDSL_Actions;
	private String SDSL_DeLaOra = "de la ora";
	
	private String[] splitLine;
	
	public SDSLLoader()
	{
		dbConector = new DBConnect("consum", "root", "");
		
		SDSL_Actions =new String[]{	"se trezeste",
									"se culca",
									"merge",
									"pleaca",
									"vine",
									"mananca",
									"bea",
									"gateste",
									"merge la WC",
									"spala",
									"porneste",
									"opreste",
									"foloseste televizor",
									"foloseste calculator",
									"foloseste masina de spalat",
									"foloseste aragaz",
									"seteaza"
									};
	}
	
	public void readFile(String path, Sdsl2BdGui gui)
	{
		try
		{
			FileInputStream fstream = new FileInputStream(path);
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			
			while ((strLine = br.readLine()) != null)
			{
				parseLine(strLine, gui);
			}
			in.close();
		}
		catch (Exception e)
		{
			System.err.println("Error: " + e.getMessage());
	    }
	}
	
	private void parseLine(String strLine, Sdsl2BdGui gui) 
	{
		String date = new String();
		String subject = new String();
		String action = new String();
		String timeStart = new String();
		String timeEnd = new String();
		
		splitLine = strLine.split("\\s");
		
		//save the date
		date = splitLine[0];
		
		//here we save the name of the subject
		subject = splitLine[1];
		
		//we set the action
		for(int i = 0; i < SDSL_Actions.length; i++)
			if( strLine.contains(SDSL_Actions[i]))
			{
				action = SDSL_Actions[i];
				break;
			}
		
		if(action.equals(null))
			System.err.println("Error: the action is unknown");
		
		//set the time
		if( strLine.contains(SDSL_DeLaOra))
		{
			timeStart = splitLine[splitLine.length - 4];
			timeEnd = splitLine[splitLine.length - 1];
		}
		else
		{
			timeStart = splitLine[splitLine.length - 1];
			timeEnd = "null";
		}

		//and here we have to insert in the data base a new entry with the four values
		gui.reportText.append("Insert in SDSL table : \n\tSubject = " + subject + "\n\tAction = " + action + "\n\tDate = " + date + "\n\tStart Time = " + timeStart + "\n\tEnd Time = " + timeEnd + "\n\n");
		
		if((subject != "null") && (action != "null") && (timeStart != "null"))
			dbConector.insert(subject, action, date, timeStart, timeEnd);
	}	
}
